App.controller('newStopResourceController', ['$rootScope', '$scope', '$state', 'Page', function ($rootScope, $scope, $state, Page) {
	Page.ajaxGetQuiet('media/stop/mediatypes.do', null, function (resp) {
        $scope.mediaTypes = resp.data;
    });
	
	Page.ajaxPostQuiet('customer/queryCustomer.do', null, function (resp) {
          $scope.customerList = resp.data;
    });

	$scope.reset = function() {
		$scope.mediaResourceGrid.reset();
	};

	$scope.mediaResourceGrid = {
		mediaType:'',
		modelType:'',
		options : {
    		bAutoWidth:false
		},
		column_defs: [
			
		],
		initDynamicTable : function(mediaId,modelType){
			this.mediaType = mediaId;
			this.modelType = modelType;
			var stopThRows = [];
	        stopThRows[0] = {style: "width: '20px'",col: '_index',name: ''};
	        stopThRows[1] = {style: "width: '70px'",col: 'stopName',name: '站点名称'};
	        stopThRows[2] = {style: "width: '70px'",col: 'roadName',name: '街道'};
	        stopThRows[3] = {style: "width: '70px'",col: 'direction',name: '行向'};
	        stopThRows[4] = {style: "width: '70px'",col: 'stopCounty',name: '区域',option:'AREA_COUNTY'};
	        
	        stopThRows[5] = {style: "width: '70px'",col: 'resourceCode',name: '资源编号'};
	        stopThRows[6] = {style: "width: '70px'",col: 'customerName',name: '经营单位'};
			stopThRows[7] = {style: "width: '70px'",col: 'contractNo',name: '合同号'};
	        //stopThRows[7] = {style: "width: '70px'",col: 'startDate',name: '起始日期'};
	        //stopThRows[8] = {style: "width: '70px'",col: 'endDate',name: '截止日期'};
	        //stopThRows[9] = {style: "width: '70px'",col: 'model',name: '规格'};
	        //stopThRows[10] = {style: "width: '70px'",col: 'relourceLevel',name: '级别',option:'RESOURCE_LEVEL'};
			if(this.modelType == "1" || this.modelType == undefined) {
				stopThRows[8] = {style: "width: '70px'",col: 'content',name: '广告内容'};
				stopThRows[9] = {style: "width: '70px'",col: 'publishTime',name: '开始时间'};
				stopThRows[10] = {style: "width: '70px'",col: 'publishOffTime',name: '结束时间'};
			}
	        
	        Page.ajaxPostQuiet('mediaType/' + mediaId + '/items.do', null, function (resp) {
        		$(resp.data).each(function(index, item) {
        			stopThRows[stopThRows.length] = {
        				style:"",
        				col:item.itemId,
        				name:item.itemName,
        				option : item.itemType == 'CODE' ? item.enumCode : false
        			}
        		});
        		$(stopThRows).each(function(index, thead) {
		        	 if(thead.col == 'stopName'){
			        	$scope.mediaResourceGrid.column_defs.push({
			        		target : index,
							template: function(row){
								return '<a href="javascript:void(0)" ed-click="viewStopInfo">'+row.stopName+'</a>'
								+ '<a href="javascript:void(0);" ed-click="viewStopPosition" title="定位" class="icon-pointer" style="float:right;padding-right: 10px;font-size:13px;"></a>';
										
							}
			        	});
		        	 }
		        	 if(thead.col == 'resourceCode'){
			        	$scope.mediaResourceGrid.column_defs.push({
			        		target : index,
							template: function(row){
								if(row.status == "0") { //媒体失效
									return  '<a href="javascript:void(0)" ed-click="viewResource">' + row.resourceCode + '</a>'
										+ '<span style="padding-left: 3px;font-size: 13px;color:red">失效</span>';
								} else {
									return  '<a href="javascript:void(0)" ed-click="viewResource">' + row.resourceCode + '</a>';
								}
							}
			        	});
		        	 }
	        	});

				$scope.mediaResourceGrid.setDynamicTable(stopThRows,{mediaType:$scope.mediaResourceGrid.mediaType,modelType : $scope.mediaResourceGrid.modelType});

        		//$scope.mediaResourceGrid.setDynamicTable(stopThRows,{mediaType:$scope.mediaResourceGrid.mediaType, modelType : $scope.mediaResourceGrid.modelType});
    		});
		},
		viewStopPosition : function(row){
			 $scope.stopPointsModal.init(row).show();
		},
		viewStopInfo : function(row){
			$scope.stopMediaInfoModal.setStopId(row.stopId).show();
		},
		viewResource : function(row){
			$scope.resourceInfoModal.open(row.resourceId);
		}
	};
	 
	$scope.$watch('mediaTypes', function (newValue, oldValue, $scope) {
		if(newValue){
        	$scope.mediaResourceGrid.initDynamicTable($scope.mediaTypes[0].mediaId,$scope.mediaTypes[0].modelType);
		}
	});
	
	$scope.resourceInfoModal = {
		open : function(resourceId){
			this.show();
			$scope.resourceInfoModal.resourceId = resourceId;
			$scope.resourceAdsGrid.query({resourceId:resourceId});
			$scope.resourceRightsGrid.query({resourceId:resourceId});
			Page.ajaxGetQuiet("media/resource/dynamicResourceDetailsHead.do",
				{mediaType : $scope.mediaResourceGrid.mediaType,resourceId : resourceId},function(resp){
					$scope.resourceItemHead = angular.extend([],resp.data.headDatas);
					$scope.resourceItemData = angular.extend({},resp.data.detailDatas[0]);
				});
		}
	};
	
	$scope.resourceAdsGrid = {
		options : {
			lazy : true
		}
	};
	
	$scope.resourceRightsGrid = {
		options : {
			lazy : true
		},
		column_defs: [

			{
				target: 5,
				buttons: [
					{
						name: '编辑',
						clazz: 'btn-edit',
						icon: 'fa-edit',
						event: function(row){
							$scope.contractFix = row;
							$scope.contractFix.contract = row.contractId;
							Page.ajaxPostQuiet('contract/contractTree.do',{resourceId : $scope.resourceInfoModal.resourceId,contractId : row.contractId},function(resp){
								$scope.contractList = resp.data;
								$scope.contractFixModal.show();
							});
						}
					}
				]
			}
		],
		addContract : function() {
			$scope.contractFix = {};
			Page.ajaxPostQuiet('contract/contractTree.do',{resourceId : $scope.resourceInfoModal.resourceId},function(resp){
				$scope.contractList = resp.data;
				$scope.contractFixModal.show();
			});
		}
	};
	
	
	$scope.stopMediaInfoModal = {
		setStopId : function(stopId){
			this.stopId = stopId;
			return this;
		},
		onShow : function(){
			Page.ajaxGetQuiet('media/stop/' + this.stopId + '/info.do', null, function (resp) {
				$scope.mediaStop = resp.data;
				Page.ajaxGetQuiet('media/stop/' + $scope.mediaStop.stopId + '/marks.do', null, function (resp) {
					$scope.mediaStop.marks = resp.data;
				});
				$scope.mediaStop.stopImgs = [];
		       	if($scope.mediaStop.stopImg && $scope.mediaStop.stopImg != ''){
		       		var imgs = $scope.mediaStop.stopImg.split('|');
		       		$(imgs).each(function(index, img) {
		       			$scope.mediaStop.stopImgs.push({
		       				path: img
		       			});
		       		});
		       	}
			});
			
			
   		}
	};
	
	$scope.stopPointsModal = {
		stops : [],
		init : function(stopData){
			this.stops = [];
			if(stopData && jQuery.isArray(stopData)){
				this.stops = stopData;
			}else if (stopData){
				this.stops.push(stopData);
			}
			return this;
		},
		onShow : function(){
			//高德
//			var stationSearch;
//			AMap.service('AMap.StationSearch',function(){//回调函数
//					stationSearch = new AMap.StationSearch({city:'027'});
//			});
			
			var mapDiv = "stopPointsModalMapDiv";
			var width = $('#' + mapDiv).width();
			var height = $('#' + mapDiv).height();
			$scope.stopPointsModal.map = new AMap.Map(mapDiv, {resizeEnable: true,zoom:13});
			
			var markers = [];
			var infoWindow = new AMap.InfoWindow({offset:new AMap.Pixel(0,-30)});
			$($scope.stopPointsModal.stops).each(function(index, stop) {
				if(stop.longitude && stop.latitude){
					var marker = new AMap.Marker({
						position: [stop.longitude, stop.latitude],
						title:stop.stopName + '-(' + stop.roadName + '-' + stop.direction + ')',
						map:$scope.stopPointsModal.map
					});
					marker.on('click',function(e){
						infoWindow.setContent(e.target.getTitle());
						infoWindow.open($scope.stopPointsModal.map, e.target.getPosition());
					});
					markers[markers.length] = marker;
				}else{
//					  stationSearch.search(stop.stopName, function(status, result) {
//			            if (status === 'complete' && result.info === 'OK' && result.stationInfo.length > 0) {
//			              	var marker = new AMap.Marker({
//								position: result.stationInfo[0].location,
//								title:result.stationInfo[0].name,
//								map:$scope.stopPointsModal.map
//							});
//							marker.on('click',function(e){
//								infoWindow.setContent(e.target.getTitle());
//								infoWindow.open($scope.stopPointsModal.map, e.target.getPosition());
//							});
//							markers[markers.length] = marker;
//			            }
//			         });
				}
			});
			
			setTimeout(function() {
				if(markers.length > 1){
					$scope.stopPointsModal.map.setFitView();
				}else if(markers.length == 1){
					$scope.stopPointsModal.map.setCenter(markers[0].getPosition());
				}
			},300);
			
			
			
		}	
	};

	$scope.contractFixModal = {

		onshow : function() {

		},
		onOkClick : function() {
			if(!$scope.contractFixForm.$valid) {
				$scope.contractFixForm.doValidate();
			} else {
				$scope.contractFix.resourceId = $scope.resourceInfoModal.resourceId;
				$scope.contractFix.customerId = $scope.contractFix.contract.customerId;
				$scope.contractFix.contractId = $scope.contractFix.contract.contractId;
				if($scope.contractFix.id == undefined || $scope.contractFix.id == "") {
					Page.ajaxPostQuiet('contract/addContractResource.do', $scope.contractFix, function (resp) {
						Page.notice("添加成功!");
						$scope.contractFixModal.hide();
						$scope.resourceRightsGrid.query({resourceId:$scope.contractFix.resourceId });
					});
				} else {
					Page.ajaxPostQuiet('contract/updateContractResource.do', $scope.contractFix, function (resp) {
						Page.notice("操作成功!");
						$scope.contractFixModal.hide();
						$scope.resourceRightsGrid.query({resourceId:$scope.contractFix.resourceId });
					});

				}
			}
		},

		onHidden : function() {
			$scope.contractFixForm.reset();
		}
	}
}]);



